import Oriented from "../Constants/Oriented";
import G from "../Utilities/Global";
import Monster from "../Entity/Monster";
import EventDefines from "../Constants/EventDefines";
import Straight from "../Components/Navigate/Straight";
import IEntity from "../Interfaces/IEntity";
import eWeaponState from "../Constants/WeaponState";
import PasteWall from "../Components/Navigate/PasteWall";
import INavigate from "../Interfaces/INavigate";

/**
 * 火球
 * created by Five on 2018-11-20 18:46:27
 */
const { ccclass, property } = cc._decorator

@ccclass
export default class Bullet extends cc.Component {
    // 控制自己的路径行为
    private _navigater: INavigate = null;
    // 起始方向
    private _oriented: Oriented;
    // 发出者
    private _owner: IEntity;

    start() {
        this.schedule(() => {
            if (this._navigater == null)
                return;
            this._navigater.settarget(this.node);
        }, 0.25);
    }

    public SetDir(ori: Oriented) {
        this._oriented = ori;
    }

    public SetOwner(own: IEntity) {
        this._owner = own;
    }
    public GetOwner() {
        return this._owner;
    }

    // 设置一下寻路组件
    public SetNavigater(ws?: eWeaponState) {
        let nav: INavigate;
        switch (ws) {
            case eWeaponState.PASTEFIRE:
                nav = new PasteWall();
                break;
            case eWeaponState.STRIGHTFIRE:
                nav = new Straight();
                break;
            default:
                nav = new Straight();
                break;
        }

        this._navigater = nav;
        this._navigater.setspeed(160);
        this._navigater.setdir(this._oriented);
    }

    public SetLifeTime(num: number) {
        this.scheduleOnce(() => {
            this.recollect();
        }, num);
    }

    // 碰撞
    onBeginContact(contact, self, other) {
        // 不管是啥好吧
        var event = new cc.Event.EventCustom(EventDefines.EVENT_BULLET_CONTACT, true);
        event.setUserData([this, other]);
        this.node.dispatchEvent(event);
        // this.recollect();
    }

    update(dt) {
        if (this._navigater == null)
            return;
        this._navigater.update(dt, this.node, null);

        if (Math.abs(this.node.position.x) - cc.winSize.width * 1.2 > 100)
            this.recollect();
    }

    recollect() {
        // TODO 播个特效
        this.destroy();
        G().Res.Put(this.node);
    }
}